﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace ContextImageResizer
{
  class Program
  {
    static string mimeType = "image/jpeg"; 

    static void Main(string[] args)
    {
     
      if (args.Length == 0)
        return;
      
      Bitmap imageFullSize = new Bitmap(args[0]);

      double aspectratio = Convert.ToDouble(Convert.ToDouble(imageFullSize.Width) / Convert.ToDouble(imageFullSize.Height)); 

      // ok now comes the dog and pony show, let's resize the image        
      Bitmap mybmp = new Bitmap(1024, Convert.ToInt32(1024 / aspectratio)); 
      
      Graphics objGraphics = Graphics.FromImage(mybmp);

      objGraphics.CompositingMode = CompositingMode.SourceOver;
      objGraphics.CompositingQuality = CompositingQuality.GammaCorrected;

      objGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

      objGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
      objGraphics.SmoothingMode = SmoothingMode.HighQuality;

      objGraphics.DrawImage(imageFullSize, 0, 0, mybmp.Width, mybmp.Height);

      Encoder qualityEncoder = Encoder.Quality;
      Encoder colorEncoder = Encoder.ColorDepth;

      EncoderParameter ratio = new EncoderParameter(qualityEncoder, 100L);
      EncoderParameter light = new EncoderParameter(colorEncoder, 24L);
      EncoderParameters codecParameters = new EncoderParameters(2);
      codecParameters.Param[0] = ratio;
      codecParameters.Param[1] = light;

      Single Brightness = 0;
      ColorMatrix BMatrix = new ColorMatrix(new Single[][]
                {new Single[] {1, 0, 0, 0, 0}, 
                 new Single[] {0, 1, 0, 0, 0}, 
                 new Single[] {0, 0, 1, 0, 0}, 
                 new Single[] {0, 0, 0, 1, 0}, 
                 new Single[] {Brightness, Brightness, Brightness, 0, 1}});


      ApplyColorMatrix(ref mybmp, BMatrix);


      // now we have to flush this to the outputstream
   
   
      mybmp.Save(args[0].ToLower().Substring(0, args[0].ToLower().LastIndexOf(".jpg")) + ".smaller.jpg", GetEncoderInfo(mimeType), codecParameters);
      objGraphics.Dispose();
      mybmp.Dispose();

    }

    private static void ApplyColorMatrix(ref Bitmap myImage, ColorMatrix myMatrix)
    {
      ImageAttributes ImgAtt = new ImageAttributes();
      Bitmap bmpMatrix = new Bitmap(myImage.Width, myImage.Height);
      System.Drawing.Graphics grMatrix = System.Drawing.Graphics.FromImage(bmpMatrix);
      ImgAtt.SetColorMatrix(myMatrix);

      /*
      Single Gamma = Convert.ToSingle(50 / 50); 
      if (Gamma > 1)
      {
        Gamma += 3 * (Gamma - 1);
      }

      ImgAtt.SetGamma(Gamma);
      */

      grMatrix.DrawImage(myImage, new Rectangle(0, 0, myImage.Width, myImage.Height), 0, 0, myImage.Width, myImage.Height, GraphicsUnit.Pixel, ImgAtt);
      myImage = bmpMatrix;
      grMatrix.Dispose();
      ImgAtt.Dispose();
    }

    private static ImageCodecInfo GetEncoderInfo(string mime)
    {

      ImageCodecInfo[] encoders;
      encoders = ImageCodecInfo.GetImageEncoders();
      foreach (ImageCodecInfo e in encoders)
      {
        if (e.MimeType == mime)
          return e;
      }

      return null;

    }
  }
}
